// pages/login/exprssorder/expressorder.js
// import common from "/utils/public.js";
import common from "../../../utils/public.js";
const db=wx.cloud.database()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth:0,
   winHeight:0,
   currentTab:0,
  //  datalist:[],
   datalist0:[],
   datalist1:[],
   datalist2:[], 
   noworder0:false,
   datax0:true,
   noworder1:false,
   datax1:true,
   noworder2:false,
   datax2:true,
   nonepickupImage:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/xszp1.jpg",
   trashImage:"cloud://linjiaan-daidai-1gqqojy38f85b825.6c69-linjiaan-daidai-1gqqojy38f85b825-1303839245/atloal-image/trash.png",
   dayuhaoImage:""
  },
  // 滑动切换tab
  bindChange:function(e){
    wx.showLoading({
      title: '数据加载中...',
    })
    var that=this;
    that.setData({currentTab:e.detail.current});
    var currentTab=that.data.currentTab
    if(currentTab==0){
      var page=that.data.datalist0.length
      that.gettxt(5,page,"未取")
    }else if(currentTab==1){
      let state="配送中"
      var page=that.data.datalist1.length
      that.gettxt(5,page,state)
    }else if(currentTab==2){
      let state="已送达"
      var page=that.data.datalist2.length
      that.gettxt(5,page,state)
    }
    wx.hideLoading()
  },
// 点击tab切换
  swichNav:function(e){
    var that=this;
    if(this.data.currentTab===e.target.dataset.current){
      return false;
    }else{
      that.setData({
        currentTab:e.target.dataset.current
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    // 获取系统信息：手机的宽度和高度
    wx.getSystemInfo({
      success: function(res){
        that.setData({
          winWidth:res.windowWidth,
          winHeight:res.windowHeight
        });
      }
    });
  },

  //获取用户的订单信息
  gettxt(num=5,page=0,state="未取"){
    var that=this
    wx.cloud.callFunction({
      name:'login',
      success(res){
        let openId=res.result.openid
        wx.cloud.callFunction({
          name:'expressget',
          data:{
            num:num,
            openid:openId,
            state:state,
            page:page
          },
          success(ress){
            if(state=="未取"){
              if(ress.result.data.length==0&&that.data.datalist0.length==0){
              that.setData({
                noworder0:true,
                datax0:false
              })
            }else if(ress.result.data.length==0){
              wx.showToast({
                title: '已无再多',
                icon:'none',
                duration:2000
              })
            } else{
              that.setData({
                noworder0:false,
                datax0:true
              })
              var oldDate=that.data.datalist0
              var newDate=oldDate.concat(ress.result.data)
              newDate.forEach(item=>{
                if(typeof(item.time)=="number"){
                  item.time=common.getMyData(item.time,"Y-m-d H:i:s")
                }
              })
              that.setData({
                datalist0:newDate
              })
            }
            }else if(state=="配送中"){
              if(ress.result.data.length==0&&that.data.datalist1.length==0){
                that.setData({
                  noworder1:true,
                  datax1:false
                })
              }else if(ress.result.data.length==0){
                wx.showToast({
                  title: '已无再多',
                  icon:'none',
                  duration:2000
                })
              }else{
                that.setData({
                  noworder1:false,
                  datax1:true
                })
                var oldDate=that.data.datalist1
                var newDate=oldDate.concat(ress.result.data)
                newDate.forEach(item=>{
                  if(typeof(item.time)=="number"){
                    item.time=common.getMyData(item.time,"Y-m-d H:i:s")
                  }
                  
                })
                that.setData({
                  datalist1:newDate
                })
              }
            }else if(state=="已送达"){
              if(ress.result.data.length==0&&that.data.datalist2.length==0){
                that.setData({
                  noworder2:true,
                  datax2:false
                })
              }else if(ress.result.data.length==0){
                wx.showToast({
                  title: '已无再多',
                  icon:'none',
                  duration:2000
                })
              }else{
                that.setData({
                  noworder2:false,
                  datax2:true
                })
                var oldDate=that.data.datalist2
                var newDate=oldDate.concat(ress.result.data)
                newDate.forEach(item=>{
                  if(typeof(item.time)=="number"){
                    item.time=common.getMyData(item.time,"Y-m-d H:i:s")
                  }
                })
                that.setData({
                  datalist2:newDate
                })
              }
            }
          },
        })
      }
    })
  },
  // 点击删除操作：
  clickRow(res){
    //获取点击的id值
    var that=this
    var id=res.currentTarget.dataset.id
    var idx=res.currentTarget.dataset.idx
    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗',
      success (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '数据删除中',
            mask:true
          })
          wx.cloud.callFunction({
            name:'expressGetphone',
            data:{
              id:id
            },
            success(res){
              let fileId=res.result.data.image
              wx.cloud.callFunction({
                name:'deleteimage',
                data:{
                  fileid:fileId
                },
                success(ress){
                  wx.cloud.callFunction({
                    name:'expressdelete',
                    data:{
                      id:id
                    },
                    success(rex){
                      wx.hideLoading({
                        success: (res) => {
                          //重新渲染
                          that.againtxt(idx)
                          wx.showToast({
                            title: '取消成功',
                            icon:'success',
                            duration:2000
                          })
                        },
                      })
                    }
                  })
                }
              })
            }
          })

         
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  //点击下单
  onorder(){
   wx.switchTab({
     url: '/pages/express/express',
   })
  },
  //点击签收
  signIN(res){
    var that=this
    var id=res.currentTarget.dataset.id
    var idx=res.currentTarget.dataset.idx

    wx.showModal({
      title: '提示',
      content: '是否确认签收',
      success (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '数据加载中...',
          })
          wx.cloud.callFunction({
            name:'express-update',
            data:{
              id:id,
              text:"已送达"
            },
            success(rex){
              that.againtxt(idx)
              that.getluserTime(id)
              that.addDictionary(id)
              wx.showToast({
                title: '确认成功',
                icon:'success',
                duration:2000
              })
              wx.hideLoading()
            }
          })
        } else if (res.cancel) {
        }
      }
    })
  },
  // 生成用户售后表
  addDictionary(expressid){
    db.collection('express').doc(expressid).get().then(res=>{
      console.log(res)
      wx.cloud.callFunction({
        name:'add-Dictionary',
        data:{
          caballero:res.data.caballero,
          expressadd:res.data.expressadd,
          image:res.data.image,
          money:res.data.money,
          pickupnum:res.data.pickupnum,
          state:res.data.state,
          time:res.data.time,
          useradd:res.data.useradd,
          _id:res.data._id,
          userid:res.data._openid,
          // _openid:res.data._openid,
          caballero_time:res.data.caballero_time,
          usersure_time:res.data.usersure_time
        },
        success(ress){
          console.log(ress)
        }

      })
    })
  },



  //点击取件按钮、获取取件骑手取件时间
getluserTime(expressid){
  wx.cloud.callFunction({
    name:'express-user_timeUpdate',
    data:{
      id:expressid,
      usersure_time:Date.parse(new Date())/1000
    }
  })
},


  //重新渲染数据
  againtxt(idx){
    var that=this
    var currentTab=that.data.currentTab
    if(currentTab==0){
       var newdata=that.data.datalist0
       newdata.splice(idx,1)
       that.setData({
         datalist0:newdata
       })
    }else if(currentTab==1){
       var newdata=that.data.datalist1
       newdata.splice(idx,1)
       that.setData({
         datalist1:newdata
       })
    }else if(currentTab==2){
       var newdata=that.data.datalist2
       newdata.splice(idx,1)
       that.setData({
         datalist2:newdata
       })
    }
   
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    wx.showLoading({
      title: '数据加载中...',
    })
      var that=this
      that.gettxt()
      wx.hideLoading()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currentTab=this.data.currentTab
    wx.showLoading({
      title: '数据加载中',
      mask:true
    })
    if(currentTab==0){
       var page=this.data.datalist0.length
       this.gettxt(5,page,"未取")
    }else if(currentTab==1){
       var page=this.data.datalist1.length
       this.gettxt(5,page,"配送中")
    }else if(currentTab==2){
      var page=this.data.datalist2.length
       this.gettxt(5,page,"已送达")
    }
   
    wx.hideLoading()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})